package cn.schoolwow.workflow.test.module.general.testcase;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.dto.FlowContextDataRange;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.QuickWorkFlow;
import cn.schoolwow.workflow.domain.instance.ManualWorkFlowAssigner;
import cn.schoolwow.workflow.domain.instance.StartWorkFlowRequest;
import cn.schoolwow.workflow.domain.instance.WorkFlowInstanceStatus;
import cn.schoolwow.workflow.domain.task.TaskType;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskQuery;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskResponse;
import cn.schoolwow.workflow.entity.WorkFlowInstance;
import org.junit.Assert;

import java.util.ArrayList;
import java.util.List;

public class StartManualAssignerAuditTestCaseFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        checkManualWorkFlowAssignerList(flowContext);
        startWorkFlow(flowContext);
        audit(flowContext);
        checkStatus(flowContext);
    }

    @Override
    public String name() {
        return "请假流程手动选择审批人工作流测试用例";
    }

    private void checkManualWorkFlowAssignerList(FlowContext flowContext){
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);

        String definitionName = "请假流程手动选择审批人工作流";
        flowContext.putCurrentFlowData("definitionName", definitionName);

        List<ManualWorkFlowAssigner> manualWorkFlowAssignerList = quickWorkFlow.getManualWorkFlowAssignerList(definitionName);
        Assert.assertEquals(2, manualWorkFlowAssignerList.size());

        for(int i=0;i<manualWorkFlowAssignerList.size();i++){
            manualWorkFlowAssignerList.get(i).assignerList.add("自选分配人"+i);
        }
        flowContext.putCurrentFlowData("manualWorkFlowAssignerList", manualWorkFlowAssignerList);
    }

    private void startWorkFlow(FlowContext flowContext){
        List<ManualWorkFlowAssigner> manualWorkFlowAssignerList = flowContext.checkData("manualWorkFlowAssignerList", List.class);
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);

        StartWorkFlowRequest startWorkFlowRequest = new StartWorkFlowRequest();
        startWorkFlowRequest.definitionName = "请假流程手动选择审批人工作流";
        startWorkFlowRequest.initiator = "张三";
        startWorkFlowRequest.manualWorkFlowAssignerList.addAll(manualWorkFlowAssignerList);
        flowContext.putInstanceData(startWorkFlowRequest, FlowContextDataRange.ContextThreadLocal);

        WorkFlowInstance workFlowInstance = quickWorkFlow.startWorkFlow(startWorkFlowRequest);
        flowContext.putInstanceData(workFlowInstance);
    }

    private void audit(FlowContext flowContext){
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);
        List<ManualWorkFlowAssigner> manualWorkFlowAssignerList = flowContext.checkData("manualWorkFlowAssignerList", List.class);

        WorkFlowTaskQuery workFlowTaskQuery = new WorkFlowTaskQuery();

        List<String> assignerList = new ArrayList<>();
        assignerList.addAll(manualWorkFlowAssignerList.get(0).assignerList);
        assignerList.add("部门主管");
        for(String assigner:assignerList){
            workFlowTaskQuery.assignerList.clear();
            workFlowTaskQuery.assignerList.add(assigner);
            WorkFlowTaskResponse workFlowTaskResponse = quickWorkFlow.getSingleWorkFlowTask(workFlowTaskQuery);
            quickWorkFlow.completeTask(workFlowTaskResponse.taskId, TaskType.COMPLETE);
        }
    }

    private void checkStatus(FlowContext flowContext){
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);
        WorkFlowInstance workFlowInstance = flowContext.checkInstanceData(WorkFlowInstance.class);

        WorkFlowInstance currentWorkFlowInstance = quickWorkFlow.getSingleWorkFlowInstance(workFlowInstance.getId());
        Assert.assertEquals(WorkFlowInstanceStatus.Finished, WorkFlowInstanceStatus.getWorkFlowInstanceStatusByStatus(currentWorkFlowInstance.getStatus()));
    }
}
